﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Security;
using System.Security.Cryptography;
using System.IO;
using System.Threading;

namespace ThreeDES
{
    public static class ThreeDES
    {
        private static byte[] strToToHexByte(string hexString)
        {
            hexString = hexString.Trim();
            if ((hexString.Length % 2) != 0)
                hexString += " ";
            byte[] returnBytes = new byte[hexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
            return returnBytes;
        }
        public static string byteToHexStr(byte[] bytes)
        {
            string returnStr = "";
            if (bytes != null)
            {
                for (int i = 0; i < bytes.Length; i++)
                {
                    returnStr += bytes[i].ToString("X2");
                }
            }
            return returnStr;
        }
        public static string byteTohex(byte[] src)
        {
            byte[] trim = { byte.Parse("20") };
            string trimbyte = Encoding.GetEncoding("GBK").GetString(trim);
            return Encoding.GetEncoding("GBK").GetString(src).Replace(trimbyte, "");

        }
        public static byte[] getTrueByte(byte[] src)
        {
            int length = src.Length;
            int residue = length % 8;
            int blank = 8 - residue;
            byte[] newSrc = new byte[length + blank];
            {
                for (int i = 0; i < src.Length; i++)
                {
                    newSrc[i] = src[i];
                }
                for (int j = 0; j < blank; j++)
                {
                    newSrc[length + j] = byte.Parse("20");
                }
                return newSrc;
            }
        }
        public static string Encrypt3DES(string   a_strString, string a_strKey)
        {
            TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();

            DES.Key = strToToHexByte(a_strKey);
            DES.Mode = CipherMode.ECB;
            DES.Padding = System.Security.Cryptography.PaddingMode.None;
            ICryptoTransform DESEncrypt = DES.CreateEncryptor();

            byte[] buffer = Encoding.GetEncoding("GBK").GetBytes(a_strString);
            byte[] Buffer = buffer;
            if (buffer.Length % 8 != 0)
                Buffer = getTrueByte(buffer);
            byte[] result = DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length);
            return byteToHexStr(result);
        }
    

        public static string Decrypt3DES(string a_strString, string a_strKey)
        {
            TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();

            DES.Key = strToToHexByte(a_strKey);
            DES.Mode = CipherMode.ECB;
            DES.Padding = System.Security.Cryptography.PaddingMode.None;

            ICryptoTransform DESDecrypt = DES.CreateDecryptor();



            byte[] Buffer = strToToHexByte(a_strString);
            byte[] result = DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length);

            return byteTohex(result);
        }

        /* static void Main(string[] args)
         {
             string key = "A2F167FDBCBF2A0D70B0FDD5C81CF8C71991409D2C2F1CE3";
             string str = "加密测试";

             string encryptStr = Encrypt3DES(str,key) ;
             Console.WriteLine("加密结果为：" + encryptStr) ;
             Console.Write("解密结果为：" + Decrypt3DES(encryptStr,key) );
            
            
             Console.ReadLine();
         }*/
    }
}